package com.example.t410.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.t410.model.TestReturn;
import com.example.t410.service.PsychologicalTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/test")
public class PsychologicalTestController {

    JSONObject jsonObject = new JSONObject();

    @Autowired
    private PsychologicalTestService psychologicalTestService;

    @GetMapping("/getAll")
    public JSONObject getAll()
    {
        jsonObject.clear();
        jsonObject.put("status","200");
        jsonObject.put("data",psychologicalTestService.findAll());
        return jsonObject;
    }

    @PostMapping("/submit")
    public JSONObject submit(@RequestBody JSONObject json)
    {
        jsonObject.clear();
        String ans = json.getString("ans");
        TestReturn ans1 = psychologicalTestService.calculateAns(ans);
        if (ans1 != null) {
            jsonObject.put("status", 200);
            jsonObject.put("data", ans1);
        } else {
            jsonObject.put("status", 201);
            jsonObject.put("data", null);
        }
        System.out.println(ans1);
        return jsonObject;
    }
}
